import { router } from '@/router/index'
import type { MenuItem } from '@/store/modules/menu'

const loadView = import.meta.glob('../views/**/*.vue')

// 动态添加路由
export function addRoutes (menu: MenuItem[]) {
  menu.forEach(e => {
    // 只将页面信息添加到路由中
    if (!e.children || e.children.length === 0) {
      // 添加缓存属性，缓存的缺点：当从未缓存的页面进入已缓存的页面时，该缓存的页面仍然会刷新
      router.addRoute('layout', {
        name: e.name,
        path: e.path as string,
        meta: {
          title: e.title,
          keepAlive: !!e.keepAlive
        },
        component: loadView[`../views${e.component}.vue`]
      })
    } else {
      addRoutes(e.children)
    }
  })
}
